﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Cartoons.Controls;
using Cartoons.ViewModels;
using System.Windows.Data;
using System.Windows.Browser;

namespace Cartoons
{
    public partial class MainPage : UserControl
    {
        private MainPageViewModel viewModel;
        public MainPage()
        {
            InitializeComponent();
            viewModel = new MainPageViewModel();
            this.DataContext = viewModel;
        }

        bool isAutomaticallyTextChanged = false;

        private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            var tb = sender as TextBox;
            tb.GetBindingExpression(TextBox.TextProperty).UpdateSource();

            if (isAutomaticallyTextChanged)
            {
                isAutomaticallyTextChanged = false;
                return;
            }

            var lb = tb.FindName("lbWords") as ListBox;
            var scroll = lb.GetScrollHost();

            if (string.IsNullOrEmpty(tb.Text))
            {
                lb.SelectedItem = null;
                scroll.ScrollToTop();
                lb.UpdateLayout();
                return;
            }

            var firstMatching = lb.Items.FirstOrDefault(i => (i as string).ToLower().StartsWith(tb.Text.ToLower()));

            if (firstMatching == null)
            {
                lb.SelectedItem = null;
                return;
            }

            lb.SelectedItem = firstMatching;
            scroll.ScrollToVerticalOffset(lb.Items.IndexOf(firstMatching));
            lb.UpdateLayout();
        }

        private void lbWords_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            isAutomaticallyTextChanged = true;
            var tb = (sender as ListBox).FindName("tbText") as TextBox;
            tb.Text = (sender as ListBox).SelectedItem.ToString();
        }

        private void TextBlock_MouseEnter(object sender, MouseEventArgs e)
        {
            (ToolTipService.GetToolTip((sender as TextBlock)) as ToolTip).IsOpen = true;
        }

        private void TextBlock_MouseLeave(object sender, MouseEventArgs e)
        {
            (ToolTipService.GetToolTip((sender as TextBlock)) as ToolTip).IsOpen = false;
        }


        private void Button_Click(object sender, RoutedEventArgs e)
        {
            viewModel.ClearFilter.Execute((sender as Button).DataContext as MainPageViewModel.FilterViewModel);
        }

        private void print_Click(object sender, RoutedEventArgs e)
        {
        }



        #region DoubleClick
        Books.Model.Book lastSelected;
        private DateTime? lastClickTime;

        private void dataGrid_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (lastClickTime != null && lastSelected != null && lastSelected == viewModel.SelectedBook && (DateTime.Now - lastClickTime.Value).TotalMilliseconds < 400)
            {
                if (this.viewModel.SelectedBook != null)
                    VisualStateManager.GoToState(this, "On", true);
            }
            lastSelected = viewModel.SelectedBook;
            lastClickTime = DateTime.Now;
        }

        #endregion

        private void about_Click(object sender, RoutedEventArgs e)
        {
            HtmlPage.Window.Navigate(new Uri("http://rarebooks.univ.net.ua/about.html", UriKind.Absolute));
        }
    }

    public class NotNullVisibilityValueConverter : IValueConverter
    {
        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return value != null ? Visibility.Visible : Visibility.Collapsed;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }

    public class BoolVisibilityValueConverter : IValueConverter
    {
        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return (value != null && (bool)value) ? Visibility.Visible : Visibility.Collapsed;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }

    public class ContentTemplateSelector : ContentControl
    {
        protected override void OnContentChanged(object oldContent, object newContent)
        {
            base.OnContentChanged(oldContent, newContent);

            if (newContent is MainPageViewModel.TextFilterViewModel)
                this.ContentTemplate = this.Resources["text"] as DataTemplate;
            else if (newContent is MainPageViewModel.RangeFilterViewModel)
                this.ContentTemplate = this.Resources["range"] as DataTemplate;
            else if (newContent is MainPageViewModel.CategoryFilterViewModel)
                this.ContentTemplate = this.Resources["category"] as DataTemplate;
        }
    }


}
